package com.heima.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dtos.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.util.BeanHelper;
import com.heima.common.util.JwtUtils;
import com.heima.model.admin.dtos.LoginResponseDTO;
import com.heima.model.user.dtos.AppLoginDto;
import com.heima.model.user.dtos.UserInfoDto;
import com.heima.model.user.enums.ApUserFlagEnum;
import com.heima.user.mapper.ApUserMapper;
import com.heima.user.pojo.ApUser;
import com.heima.user.service.ApUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * <p>
 * APP用户信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2023-03-22
 */
@Service
public class ApUserServiceImpl extends ServiceImpl<ApUserMapper, ApUser> implements ApUserService {

    /**
     * 根据id修改App用户flag
     *
     * @param id
     */

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

    @Override
    public void updateToWemediaUser(Integer id) {
        ApUser apUser = new ApUser();
        apUser.setId(id);
        apUser.setStatus(true);
        apUser.setFlag(ApUserFlagEnum.WEMEDIA.getCode());
        apUser.setIsIdentityAuthentication(true);
        apUser.setIsCertification(true);
        this.updateById(apUser);
    }

    /**
     * App用户登录 允许匿名登陆
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult<LoginResponseDTO<UserInfoDto>> login(AppLoginDto dto) {
        // 如果手机号和密码都不为空,则进行登陆判断
        if (StringUtils.isNotBlank(dto.getPhone()) && StringUtils.isNotBlank(dto.getPassword())) {
            LambdaQueryWrapper<ApUser> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(ApUser::getPhone, dto.getPhone());
            ApUser apUserDb = this.getOne(queryWrapper);
            // 用户名不存在
            if (apUserDb == null || apUserDb.getPassword() == null) {
                throw new LeadException(AppHttpCodeEnum.AP_USER_DATA_NOT_EXIST);
            }

            String passwordDb = apUserDb.getPassword();
            String passwordRequest = dto.getPassword();
            boolean isMatches = passwordEncoder.matches(passwordRequest, passwordDb);
            if (isMatches) {
                String token = JwtUtils.generateTokenExpireInMinutes(apUserDb.getId(), 120);
                return ResponseResult.ok(new LoginResponseDTO<>(token, BeanHelper.copyProperties(apUserDb, UserInfoDto.class)));
            } else {
                throw new LeadException(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
            }

        }
        // 否则认为是匿名登陆 用户id设为-1
        String token = JwtUtils.generateTokenExpireInMinutes(-1, 120);
        return ResponseResult.ok(new LoginResponseDTO<>(token, null));
    }
}
